package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.AuthStatusEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.engine.cache.redis.RedisHelper;
import com.bifang.engine.data.core.util.PagerUtil;
import com.bifang.module.base.common.vo.dict.DictAreaVO;
import com.bifang.module.base.common.vo.dict.SelectVO;
import com.bifang.module.base.core.helper.DictHelper;
import com.bifang.module.base.core.integration.DictIntegration;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.enums.EnrollStatusFormalEnum;
import com.jwsoft.manager.common.enums.TjTypeEnum;
import com.jwsoft.manager.common.vo.eduSchool.EduSchoolVO;
import com.jwsoft.manager.common.vo.eduSchoolConfig.EduSchoolConfigVO;
import com.jwsoft.manager.common.vo.eduSchoolTj.StudentEnrollTjVO;
import com.jwsoft.manager.common.vo.eduSchoolTjParams.EduSchoolTjParamsKeyVO;
import com.jwsoft.manager.common.vo.eduSchoolTjParams.EduSchoolTjParamsQueryVO;
import com.jwsoft.manager.common.vo.eduSchoolTjParams.EduSchoolTjParamsVO;
import com.jwsoft.manager.core.dao.model.EduSchoolTjParams;
import com.jwsoft.manager.core.dao.model.EduStudentEnroll;
import com.jwsoft.manager.core.dao.model.EduStudentTj;
import com.jwsoft.manager.core.dao.service.EduSchoolService;
import com.jwsoft.manager.core.dao.service.EduSchoolTjParamsService;
import com.jwsoft.manager.core.dao.service.EduStudentEnrollService;
import com.jwsoft.manager.core.dao.service.EduStudentTjService;
import com.jwsoft.manager.core.integration.EduSchoolTjParamsIntegration;
import com.jwsoft.manager.core.util.EduHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 学校调剂参数管理业务实现类
 *
 * @author chenzh
 * @since 2023-07-04
 */
@ApiService(funcCode = "eduSchoolTjParams", title = "学校调剂参数管理")
public class EduSchoolTjParamsIntegrationImpl implements EduSchoolTjParamsIntegration {
    @Autowired
    private EduSchoolService eduSchoolService;
    @Autowired
    public EduSchoolTjParamsService eduSchoolTjParamsService;
    @Autowired
    private EduHelper eduHelper;
    @Autowired
    private DictHelper dictHelper;
    @Autowired
    private EduStudentEnrollService eduStudentEnrollService;
    @Autowired
    private EduStudentTjService eduStudentTjService;

    @Autowired
    private RedisHelper redisHelper;
    @Autowired
    private DictIntegration dictIntegration;

    @Override
    @OpApi(funcCode = "eduSchoolTjParams0001", title = "学校调剂参数管理分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduSchoolTjParamsVO> getList(EduSchoolTjParamsQueryVO vo) {
        if (ObjectUtils.isEmpty(vo.getYear())){
            throw new AppException("招生年度不能为空！");
        }
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(20);
        }
        List<String> areaCodeList = new ArrayList<>();
        if (ObjectUtils.isEmpty(vo.getAreaCode())){
            List<SelectVO> selectVOList = dictIntegration.getDistrictByOrg(new DictAreaVO());
            areaCodeList = selectVOList.stream()
                    .map(SelectVO::getDictKey)
                    .collect(Collectors.toList());
            String sessionAreaCode = SessionUtil.getAreaCode();
            if (!areaCodeList.contains(sessionAreaCode)) {
                areaCodeList.add(sessionAreaCode);
            }
        }else {
            areaCodeList.add(vo.getAreaCode());
        }
        if (CollectionUtils.isEmpty(areaCodeList)){
            throw new AppException("必须传入行政区划");
        }
        QueryWrapper<EduSchoolTjParams> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EduSchoolTjParams::getYear, vo.getYear())
                .in( EduSchoolTjParams::getAreaCode, areaCodeList)
                .eq(StringUtils.isNotBlank(vo.getFlag()), EduSchoolTjParams::getFlag, vo.getFlag())
                .eq(StringUtils.isNotBlank(vo.getFromSchoolId()), EduSchoolTjParams::getFromSchoolId, vo.getFromSchoolId())
                .eq(StringUtils.isNotBlank(vo.getToSchoolId()), EduSchoolTjParams::getToSchoolId, vo.getToSchoolId()).orderByAsc(EduSchoolTjParams::getFromSchoolId, EduSchoolTjParams::getCreateTime);

        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<EduSchoolTjParams> list = eduSchoolTjParamsService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduSchoolTjParams> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduSchoolTjParamsVO> resultList = Convert.toList(EduSchoolTjParamsVO.class, pageInfo.getList());
        // 转义数据字典值
        for (EduSchoolTjParamsVO eduSchoolTjParamsVO : resultList) {
            eduSchoolTjParamsVO.setAreaName(dictHelper.getAreaNameByCode(eduSchoolTjParamsVO.getAreaCode()));
        }
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduSchoolTjParams0002", title = "学校调剂参数管理根据主键查询详情", funcType = FuncTypeEnum.query)
    public EduSchoolTjParamsVO getById(EduSchoolTjParamsKeyVO vo) {
        EduSchoolTjParams entity = eduSchoolTjParamsService.getById(vo.getSchoolTjId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduSchoolTjParamsVO result = Convert.convert(EduSchoolTjParamsVO.class, entity);
        return result;
    }

    /**
     * 保存前统一校验
     */
    @Override
    @OpApi(funcCode = "eduSchoolTjParams0003", title = "学校调剂参数管理保存前校验", funcType = FuncTypeEnum.other, checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduSchoolTjParamsVO vo) {
        if (StringUtils.isBlank(vo.getFromSchoolId())) {
            throw new AppException("调出学校不能为空");
        }
        if (StringUtils.isBlank(vo.getToSchoolId())) {
            throw new AppException("调入学校不能为空");
        }
        if (vo.getTjCount() == null) {
            throw new AppException("调剂人数不能为空");
        }
        if (vo.getFromSchoolId().equals(vo.getToSchoolId())) {
            throw new AppException("调出和调入学校不能一致");
        }
        if (vo.getYear() == null) {
            Integer year = eduHelper.thisYear();
            vo.setYear(year);
        }
        EduSchoolVO eduSchoolVO = eduSchoolService.getDetail(vo.getFromSchoolId());
        if (eduSchoolVO == null) {
            throw new AppException("调出学校不存在");
        }
        if (StringUtils.isBlank(vo.getAreaCode())) {
            vo.setAreaCode(eduSchoolVO.getAreaCode());
        }
        EduSchoolVO toSchool = eduSchoolService.getDetail(vo.getToSchoolId());
        if (toSchool == null) {
            throw new AppException("调入学校不存在");
        }
        if (!toSchool.getSchoolCategory().equalsIgnoreCase(eduSchoolVO.getSchoolCategory())) {
            throw new AppException("调入调出学校学段不一致");
        }
    }

    @Override
    @OpApi(funcCode = "eduSchoolTjParams0004", title = "学校调剂参数管理新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduSchoolTjParams0004",
            operationName = "学校调剂参数管理新增",
            dataType = "schoolTjId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduSchoolTjParamsVO add(EduSchoolTjParamsVO vo) {
        //保存前校验
        checkSave(vo);
        QueryWrapper<EduSchoolTjParams> tjQueryWrapper = new QueryWrapper<>();
        tjQueryWrapper.lambda().eq(EduSchoolTjParams::getYear, vo.getYear())
                .eq(EduSchoolTjParams::getFromSchoolId, vo.getFromSchoolId())
                .eq(EduSchoolTjParams::getToSchoolId, vo.getToSchoolId());
        if (eduSchoolTjParamsService.count(tjQueryWrapper) > 0) {
            throw new AppException("学校（" + vo.getFromSchoolName() + "）调剂到学校（" + vo.getToSchoolName() + "）已存在，如有问题，请修改");
        }
        vo.setSchoolTjId(IdUtil.simpleUUID());
        //保存数据
        EduSchoolTjParams entity = Convert.convert(EduSchoolTjParams.class, vo);
        if (StringUtils.isBlank(entity.getFlag())) {
            //新增默认有效状态
            entity.setFlag(BoolEnum.TRUE.getType());
        }
        eduSchoolTjParamsService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setSchoolTjId(entity.getSchoolTjId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduSchoolTjParams0005", title = "学校调剂参数管理修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduSchoolTjParams0005",
            operationName = "学校调剂参数管理修改",
            dataType = "schoolTjId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduSchoolTjParamsVO edit(EduSchoolTjParamsVO vo) {
        if (ObjectUtils.isEmpty(vo.getSchoolTjId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduSchoolTjParams oldEntity = eduSchoolTjParamsService.getById(vo.getSchoolTjId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        QueryWrapper<EduSchoolTjParams> tjQueryWrapper = new QueryWrapper<>();
        tjQueryWrapper.lambda().eq(EduSchoolTjParams::getYear, vo.getYear())
                .ne(EduSchoolTjParams::getSchoolTjId, vo.getSchoolTjId())
                .eq(EduSchoolTjParams::getFromSchoolId, vo.getFromSchoolId())
                .eq(EduSchoolTjParams::getToSchoolId, vo.getToSchoolId());
        if (eduSchoolTjParamsService.count(tjQueryWrapper) > 0) {
            throw new AppException("学校（" + vo.getFromSchoolName() + "）调剂到学校（" + vo.getToSchoolName() + "），不能重复");
        }
        //保存数据
        EduSchoolTjParams entity = Convert.convert(EduSchoolTjParams.class, vo);
        eduSchoolTjParamsService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduSchoolTjParams0006", title = "学校调剂参数管理根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "eduSchoolTjParams0006",
            operationName = "学校调剂参数管理删除",
            dataType = "schoolTjId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduSchoolTjParamsKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getSchoolTjId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduSchoolTjParams entity = eduSchoolTjParamsService.getById(vo.getSchoolTjId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduSchoolTjParamsService.removeById(vo.getSchoolTjId());
    }

    @Override
    @OpApi(funcCode = "eduSchoolTjParams0008", title = "义务教育学校调剂保存", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduSchoolTjParams0008",
            operationName = "义务教育学校调剂保存",
            dataType = "studentEnrollId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void schoolTj(StudentEnrollTjVO vo) {
        if (StringUtils.isBlank(vo.getStudentEnrollId())) {
            throw new AppException("报名id不能为空");
        }
        if (StringUtils.isBlank(vo.getSchoolId())) {
            throw new AppException("调剂目标学校不能为空");
        }
        if (vo.getManagerFlag()==null){
            vo.setManagerFlag(false);
        }
        EduStudentEnroll enroll = eduStudentEnrollService.getById(vo.getStudentEnrollId());
        if (enroll == null) {
            throw new AppException("报名信息不存在");
        }
        if (!vo.getManagerFlag()&&EnrollStatusFormalEnum.xtblq.getType().equals(enroll.getEnrollStatus())) {
            throw new AppException("报名无效学生不能调剂");
        }
        if (!vo.getManagerFlag()&&AuthStatusEnum.NO.getType().equals(enroll.getAuthStatus())) {
            throw new AppException("审核不通过学生不能调剂");
        }
        QueryWrapper<EduStudentTj> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EduStudentTj::getStudentEnrollId, vo.getStudentEnrollId())
                .eq(EduStudentTj::getFromSchoolId, enroll.getSchoolId())
                .eq(EduStudentTj::getCurrentFlag, BoolEnum.TRUE.getType());
        EduStudentTj eduStudentCheck = eduStudentTjService.getOne(queryWrapper, false);
        if (eduStudentCheck != null) {
            if (AuthStatusEnum.NODO.getType().equalsIgnoreCase(eduStudentCheck.getAuthStatus())) {
                throw new AppException("家长已申请调剂，不能直接调剂，请通过审核功能调剂");
            }
            eduStudentCheck.setCurrentFlag(BoolEnum.FALSE.getType());
            eduStudentTjService.updateById(eduStudentCheck);
        }
        QueryWrapper<EduSchoolTjParams> tjQueryWrapper = new QueryWrapper<>();
        tjQueryWrapper.lambda().eq(EduSchoolTjParams::getFromSchoolId, enroll.getSchoolId())
                .eq(EduSchoolTjParams::getToSchoolId, vo.getSchoolId())
                .eq(EduSchoolTjParams::getYear, enroll.getYear());
        List<EduSchoolTjParams> tjList = eduSchoolTjParamsService.list(tjQueryWrapper);
        if (CollectionUtils.isEmpty(tjList)) {
            throw new AppException("未配置调剂名额，请联系管理员");
        }
        if (tjList.size() > 1) {
            throw new AppException("学校调剂参数配置有误，存在多条数据，请联系管理员");
        }
        EduSchoolTjParams tj = tjList.get(0);
        Integer tjcount = tj.getTjCount();
        if (tjcount == null) {
            throw new AppException("未配置调剂名额，请联系管理员");
        }
        QueryWrapper<EduStudentTj> studentTjQueryWrapper = new QueryWrapper<>();
        studentTjQueryWrapper.lambda().eq(EduStudentTj::getFromSchoolId, enroll.getSchoolId())
                .eq(EduStudentTj::getTjType, TjTypeEnum.xx.getType())
                .eq(EduStudentTj::getToSchoolId, vo.getSchoolId());
        long tjrs = eduStudentTjService.count(studentTjQueryWrapper);
        if (tjrs >= tjcount&&!vo.getManagerFlag()) {
            throw new AppException("已达调剂人数上线，不能保存");
        }
        //判断是否超过计划数
        EduSchoolVO eduSchool = eduSchoolService.getDetail(vo.getSchoolId());
        QueryWrapper<EduStudentEnroll> enrollQueryWrapper = new QueryWrapper<>();
        enrollQueryWrapper.lambda().eq(EduStudentEnroll::getEnrollStatus, EnrollStatusFormalEnum.lq.getType())
                .eq(EduStudentEnroll::getSchoolId, tj.getToSchoolId())
                .eq(EduStudentEnroll::getYear, enroll.getYear());
        long lqrs = eduStudentEnrollService.count(enrollQueryWrapper);
        if (!CollectionUtils.isEmpty(eduSchool.getConfigList())) {
            EduSchoolConfigVO configVO = eduSchool.getConfigList().get(0);
            Integer jhs = configVO.getEnrolment() == null ? 0 : configVO.getEnrolment();
            if (jhs - lqrs <= 0) {
                throw new AppException("调入学校已超计划数，不能调剂");
            }
        }
        //调剂
        if (StringUtils.isBlank(enroll.getFromSchoolId())) {
            //调出学校记录的是报名时候的学校
            enroll.setFromSchoolId(enroll.getSchoolId());
            enroll.setFromSchoolName(enroll.getSchoolName());
        }
        enroll.setSchoolId(eduSchool.getOrgId());
        enroll.setSchoolName(eduSchool.getSchoolName());
        enroll.setStreet(eduSchool.getStreet());
        if(!EnrollStatusFormalEnum.dlq.getType().equals(enroll.getEnrollStatus())){
            enroll.setEnrollStatus(EnrollStatusFormalEnum.lq.getType());
            enroll.setAdmitFlag(BoolEnum.TRUE.getType());
        }

        enroll.setAdmitRemarks("调剂录取");
        enroll.setDeployFlag(BoolEnum.TRUE.getType());
        enroll.setBasis("从" + tj.getFromSchoolName() + "调剂到" + tj.getToSchoolName());
        enroll.setApplyId(this.getApplyId(enroll.getYear(), eduSchool.getSchoolNature(), eduSchool.getSchoolNum(), 97, eduSchool.getOrgId(), 1));
        eduStudentEnrollService.updateById(enroll);
        EduStudentTj eduStudentTj = Convert.convert(EduStudentTj.class, tj);
        eduStudentTj.setStudentTjId(IdUtil.simpleUUID());
        eduStudentTj.setStudentEnrollId(enroll.getStudentEnrollId());
        eduStudentTj.setFullName(enroll.getFullName());
        eduStudentTj.setIdcard(enroll.getIdcard());
        eduStudentTj.setCurrentFlag(BoolEnum.TRUE.getType());
        eduStudentTj.setAreaCode(enroll.getAreaCode());
        eduStudentTj.setAuthStatus(AuthStatusEnum.YES.getType());
        eduStudentTj.setTjType(TjTypeEnum.xx.getType());
        eduStudentTj.setConfigName(enroll.getConfigName());
        eduStudentTj.setConfigTypeName(enroll.getConfigTypeName());
        eduStudentTj.setRegisterDate(enroll.getRegisterDate());
        eduStudentTj.setQysj(enroll.getQysj());
        eduStudentTj.setCbTotal(enroll.getCbTotal());
        eduStudentTj.setStudentEnrollStatus(enroll.getEnrollStatus());
        eduStudentTj.setStudentAuthStatus(enroll.getAuthStatus());
        eduStudentTj.setStudentAdmitFlag(enroll.getAdmitFlag());
        eduStudentTjService.save(eduStudentTj);
    }

    @Override
    @OpApi(funcCode = "eduSchoolTjParams0007", title = "根据学校id查询可调剂学校信息", funcType = FuncTypeEnum.query, checkPrivilege = BoolEnum.FALSE)
    public List<EduSchoolTjParamsVO> getListBySchoolId(EduSchoolTjParamsQueryVO vo) {
        int year = eduHelper.thisYear();
        String schoolId = vo.getFromSchoolId();
        if (StringUtils.isBlank(schoolId)) {
            schoolId = SessionUtil.getSessionInfo().getOrgId();
        }
        QueryWrapper<EduSchoolTjParams> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EduSchoolTjParams::getYear, year)
                .eq(EduSchoolTjParams::getFromSchoolId, schoolId);
        List<EduSchoolTjParams> list = eduSchoolTjParamsService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<EduSchoolTjParamsVO> retList = new ArrayList<>();
        for (EduSchoolTjParams eduSchoolTj : list) {
            EduSchoolTjParamsVO eduSchoolTjVO = Convert.convert(EduSchoolTjParamsVO.class, eduSchoolTj);
            QueryWrapper<EduStudentTj> studentTjQueryWrapper = new QueryWrapper<>();
            studentTjQueryWrapper.lambda().eq(EduStudentTj::getFromSchoolId, eduSchoolTj.getFromSchoolId())
                    .eq(EduStudentTj::getToSchoolId, eduSchoolTj.getToSchoolId())
                    .eq(EduStudentTj::getYear, year)
                    .eq(EduStudentTj::getTjType, TjTypeEnum.xx.getType());
            long tjrs = eduStudentTjService.count(studentTjQueryWrapper);
            eduSchoolTjVO.setUseCount(tjrs);
            retList.add(eduSchoolTjVO);
        }

        return retList;
    }


    public  String getApplyId(Integer year,String schoolNature,String schoolNum,Integer orderNo,String shcoolId,Integer count){
        if(count==null){
            count=count+1;
        }
        if(count>10){
            //超过10次未能比对到不重复的数据，返回报错
            throw new AppException("系统繁忙，请重新点提交");
        }
        String y=year % 100+"";
        String num = StringUtils.leftPad(schoolNum, eduHelper.thisAreaCode().equalsIgnoreCase("330783")?3:9, "0");
        String orderNoId = StringUtils.leftPad(orderNo + "", 2, "0");
        String applyId=y+num+orderNoId;
        if("0".equals(schoolNature)){
            String redisKey="ywEnroll:gb:"+shcoolId+":"+orderNo;
            if(redisHelper.hasKey(redisKey)) {
                String redisApplyId= (String) redisHelper.get(redisKey);
                applyId=String.valueOf(Long.parseLong(redisApplyId)+1);
                redisHelper.set(redisKey,applyId,86400);
            }else{
                //公办
                String max=eduStudentEnrollService.queryMaxApplyId(applyId,shcoolId,year);
                if(StringUtils.isBlank(max)){
                    applyId=applyId+StringUtils.leftPad("1",  eduHelper.thisAreaCode().equalsIgnoreCase("330783")?3:4, "0");
                }else{
                    applyId=String.valueOf(Long.parseLong(max)+1);
                }
                redisHelper.set(redisKey,applyId,86400);
            }
            return applyId;
        }else{
            //民办或者公参民转公
            String redisKey="ywEnroll:mb:"+shcoolId+":"+orderNo;
            Integer randomId =null;
            if(redisHelper.hasKey(redisKey)) {
                List<Integer> randomList= (List<Integer>) redisHelper.get(redisKey);
                if(CollectionUtils.isEmpty(randomList)){
                    Integer[] randomNums= NumberUtil.generateBySet(1,9999,3000);
                    randomList= Arrays.stream(randomNums).distinct().collect(Collectors.toList());
                    randomId=randomList.get(0);
                    randomList.remove(randomId);
                    redisHelper.set(redisKey,randomList,259200);
                }else{
                    randomId=randomList.get(0);
                    randomList.remove(randomId);
                    redisHelper.set(redisKey,randomList,259200);
                }
            }else{
                Integer[] randomNums=NumberUtil.generateBySet(1,9999,3000);
                List<Integer> randomList=Arrays.stream(randomNums).distinct().collect(Collectors.toList());
                randomId=randomList.get(0);
                randomList.remove(randomId);
                redisHelper.set(redisKey,randomList,259200);
            }
            applyId=applyId+StringUtils.leftPad(randomId + "", 4, "0");

        }
        QueryWrapper<EduStudentEnroll> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().select(EduStudentEnroll::getStudentEnrollId).eq(EduStudentEnroll::getSchoolId,shcoolId)
                .eq(EduStudentEnroll::getYear,year)
                .eq(EduStudentEnroll::getApplyId,applyId);
        if(eduStudentEnrollService.count(queryWrapper)>0){
            applyId=this.getApplyId(year,schoolNature,schoolNum,orderNo,shcoolId,count);
        }
        return applyId;
    }

}
